# coding:utf-8
# coding:utf-8

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("/Library/Fonts/Courier New.ttf", 40, encoding="unic")  # 设置字体


def append_pointer_to_image(img, x, y, color=(0, 0, 0)):
    draw = ImageDraw.Draw(img)
    # 画x轴线
    draw.line((x, 0, x, img.size[1]), fill=(128, 128, 128))
    # 画y轴线
    draw.line((0, y, img.size[0], y), fill=(128, 128, 128))
    # 画点
    draw.ellipse((x - 3, y - 3, x + 3, y + 3), fill=color)
    # 写坐标
    # draw.text((x - 150, y + 20), u'({x}, {y})'.format(x=x, y=y), (0, 0, 255), font)
    del draw


def open_img(name):
    return Image.open('debug/{n}'.format(n=name))


def get_mid_color(*colors):
    return tuple(sum(a) / len(a) for a in zip(*colors))


def avg(arr):
    if len(arr) == 0:
        return 0
    else:
        return sum(arr) / len(arr)


def get_expect_point_from_points1(*points):
    _tmp = zip(*points)
    min_x, min_y = min(_tmp[0]), min(_tmp[1])
    x = avg([p[0] for p in filter(lambda p1: p1[1] == min_y, points)])
    y = avg([p[1] for p in filter(lambda p1: p1[0] == min_x, points)])
    return x, y
    # _points = filter(lambda p: p[0] == min_x or p[1] == min_y, points)
    # _tmp = zip(*_points)
    # return avg(*_tmp[0]), avg(*_tmp[1])


def get_expect_point_from_points(*points):
    _tmp = zip(*points)
    min_x, min_y = min(_tmp[0]), min(_tmp[1])
    _points = filter(lambda p: p[0] == min_x or p[1] == min_y, points)
    _tmp = zip(*_points)
    return max(_tmp[0]), max(_tmp[1])


def is_color_similar(base_color, check_color, deviation=0):
    if check_color is None:
        return False
    r, g, b, _ = base_color
    return r - deviation <= check_color[0] <= r + deviation and \
           g - deviation <= check_color[1] <= g + deviation and \
           b - deviation <= check_color[2] <= b + deviation


scan_step = 1
ch_color = (55, 58, 103)

